<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class mfriend extends MY_Model {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
    public $mytable = "friend";
    public $primaryKey = "friend_id";
    public $resCode = "FRIEND";

    function __construct() {
        parent::__construct();
    }

    function field() {
		$fieldData = array();
		$fieldData['use_one'] = 0;
		$fieldData['use_two'] = 0;
		$fieldData['friend_status'] = 0;
		$fieldData['friend_time_create'] = time();
		return $fieldData;
    }
	function insert_fb_friends($user_id, $arr_id){
		if($arr_id){
			//$this->load->model("muser");
			$this->db->select("use_id");
			$users = $this->muser->where_in($arr_id,'facebook_id');
			$lusers = $this->gets_friend($user_id);
			$arr_list_user = array();
			if($lusers)
			foreach($lusers as $luser){
				array_push($arr_list_user,$luser->use_one, $luser->use_two);
				
			}
			if($users){
				$arr_insert = array();
				foreach($users as $user){
					if(!in_array($user->use_id,$arr_list_user)){
					$arr_insert[] = array('use_one'=>$user_id,
										'use_two'=>$user->use_id
										);
					}
				}
				if(count($arr_insert) >0)
					$this->db->insert_batch($this->mytable, $arr_insert);
			}
		}
		
	}
	function gets_friend($user_id){
		$this->db->or_where('use_two',$user_id);
		return $this->gets($user_id,'use_one');
	}
	function check_friend(){
		/*
		 * SELECT 'friend_one','friend_two','status' FROM friends
			WHERE
			(friend_one="$user_id" OR friend_two="$user_id")
			AND
			(friend_one="$friend_id" OR friend_two="$friend_id")
			*/
		
	}
	
	function list_friend(){
		/*
		 * SELECT F.status, U.username, U.email
			FROM users U, friends F
			WHERE
			CASE
			
			WHEN F.friend_one = '$user_id'
			THEN F.friend_two = U.user_id
			WHEN F.friend_two= '$user_id'
			THEN F.friend_one= U.user_id
			END
			
			AND 
			F.status='1';
			*/
	}
}

?>